package com.usian.controller;

import com.usian.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class RibbonController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable Integer id){
        ServiceInstance si = loadBalancerClient.choose("nacos-provider");
       //指定所需要的服务
        String url = "http://"+si.getHost()+":"+si.getPort()+"/getUserById/"+id;
    return restTemplate.getForObject(url,User.class);
    }
}
